package com.gx.smart.building.ui.repair.fragment

import android.app.Activity
import android.content.Intent
import android.view.KeyEvent
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import android.widget.TextView
import android.widget.TextView.OnEditorActionListener
import com.blankj.utilcode.util.KeyboardUtils
import com.gx.smart.base.BaseAdapter.OnItemClickListener
import com.gx.smart.base.BaseMVVMFragmentV2
import com.gx.smart.building.R
import com.gx.smart.building.databinding.FragmentProcessingTransformSearchBinding
import com.gx.smart.building.ui.repair.adapter.RepairDepartmentAdapter
import com.gx.smart.building.ui.repair.adapter.RepairSearchPersonAdapter
import com.gx.smart.building.ui.repair.viewmodel.RepairViewModel
import com.huishi.gx.ib.grpc.lib.ib.app.sysuser.v1.SysOrganDto
import com.huishi.gx.ib.grpc.lib.ib.app.sysuser.v1.SysUserDto

class ProcessingTransformSearchFragment : BaseMVVMFragmentV2<RepairViewModel>(),
    View.OnClickListener,
    OnEditorActionListener,
    OnItemClickListener<SysOrganDto>{
    private lateinit var viewBinding: FragmentProcessingTransformSearchBinding
    private lateinit var personAdapter: RepairSearchPersonAdapter
    private lateinit var departmentAdapter: RepairDepartmentAdapter
    private var departments: List<SysOrganDto>? = null
    override fun onBindLayout(): Int = R.layout.fragment_processing_transform_search

    override fun getToolbarTitle(): String = "选择处理人"

    override fun onBindViewModel(): Class<RepairViewModel> = RepairViewModel::class.java

    override fun initContentView(root: ViewGroup) {
        viewBinding = FragmentProcessingTransformSearchBinding.inflate(layoutInflater, root, true)
    }

    override fun initContent() {
        super.initContent()
        personAdapter = RepairSearchPersonAdapter(requireContext())
        personAdapter.setItemClickListener(object : OnItemClickListener<SysUserDto> {
            override fun onItemClick(e: SysUserDto, position: Int) {
                val intent = Intent()
                intent.putExtra("id", e.uaaUserId)
                intent.putExtra("name", e.name)
                requireActivity().setResult(Activity.RESULT_OK, intent)
                requireActivity().finish()
            }
        })
        viewBinding.personRecyclerView.adapter = personAdapter
        departmentAdapter = RepairDepartmentAdapter(requireContext())
        viewBinding.departmentRecyclerView.adapter = departmentAdapter
        viewBinding.search.setOnEditorActionListener(this)
        viewBinding.clear.setOnClickListener(this)
        departmentAdapter.setItemClickListener(this)
    }

    override fun onEditorAction(v: TextView?, actionId: Int, event: KeyEvent?): Boolean {
        if (actionId == EditorInfo.IME_ACTION_SEARCH) {
            search()
            KeyboardUtils.hideSoftInput(v!!)
            return true
        }
        return false
    }

    override fun initData() {
        viewModel.sysOrganQuery().observe(viewLifecycleOwner) {
            departments = it
            departmentAdapter.addAll(it)
        }

    }

    private fun search() {
        val name = viewBinding.search.text.toString().trim()
        viewModel.sysUserQueryByName(name).observe(viewLifecycleOwner) {
            personAdapter.refresh(it)
        }
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.clear -> viewBinding.search.editableText.clear()
        }
    }

    override fun onItemClick(e: SysOrganDto, position: Int) {
        departments?.let { it ->
            departments = it[position].childrenList
            departmentAdapter.refresh(departments)
        }
        viewModel.sysUserQuery(e.id).observe(viewLifecycleOwner) {
            personAdapter.refresh(it)
        }
    }
}